﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.NetworkInformation;
using System.Text;
using com.IronOne.SLIC2.Controllers;
using System.Configuration;
using com.IronOne.IronUtils;
using com.IronOne.SLIC2.HandlerClasses;

namespace com.IronOne.SLIC2
{
    public partial class Diagnosis : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnEmailCheck_Click(object sender, EventArgs e) 
        {
            try
            {
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(ApplicationSettings.SupportInfoEmail, txtEmail.Text.Trim(), "Motor Claim- Diagnosis", "Mail generated by Motor Claim diagnosis system on request. Date :" + DateTime.Now);
                
                // Send Email
                EmailUtils handler = new EmailUtils(message);
                handler.SendEmail();

               // lblEmailResult.Text = "Email has been queued for sending. Please check the inbox. If sending was successful you should";
            }
            catch (Exception ex)
            {
                lblEmailResult.Text = "Exception Message: " + ex.Message +
                    "\n\nInner Exception: " + ex.InnerException;
            }
        }

        protected void btnConnectionCheck_Click(object sender, EventArgs e)
        {
            try
            {
                //Ping to database
                if (PingToDatabase())
                {
                    lblConnectionResult.Text = "Success";
                }
                else
                {
                    lblConnectionResult.Text = "Failed";
                }
            }
            catch (Exception ex)
            {
                lblConnectionResult.Text = "Exception Message: " + ex.Message +
                    ",\n\nInner Exception: " + ex.InnerException;
            }
        }

        /// <summary>
        /// Used to PING to the database
        /// </summary>
        /// <returns>true for success</returns>       
        public bool PingToDatabase()
        {
            try
            {
                Ping pingSender = new Ping();
                PingOptions options = new PingOptions();

                // Use the default TTL value which is 128,
                // but change the fragmentation behavior.
                options.DontFragment = true;

                // Create a buffer of 32 bytes of data to be transmitted.
                const string data = "Pinging from web server";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                int timeOut = int.Parse(ConfigurationManager.AppSettings["ping_timeOut"].ToString());
                PingReply reply = pingSender.Send(ConfigurationManager.AppSettings["database_IP"].ToString().Trim(), timeOut, buffer, options);

                if (reply.Status != IPStatus.Success)
                    throw new GenException(150, "Database connection could not be established.");
            }
            catch (Exception)
            {
                throw;
            }

            return true;
        }
    }
}